import {messages} from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import constants from 'share/constants'
import { Form, message, Icon, Tabs } from 'antd'
const TabPane = Tabs.TabPane;

import BasicInfo from 'components/basic-info'
import DetailPageAuth from 'containers/setting/role-setting/finance-role/detail-page-auth'
import DetailLegalEntity from 'containers/setting/role-setting/finance-role/detail-legal-entity'
import DetailCompanyAuth from 'containers/setting/role-setting/finance-role/detail-company-auth'
import DetailDepartmentAuth from 'containers/setting/role-setting/finance-role/detail-department-auth'
import DetailCostCenter from 'containers/setting/role-setting/finance-role/detail-cost-center'
import DetailAssignCenter from 'containers/setting/role-setting/finance-role/detail-assign-user'
import RSService from 'containers/setting/role-setting/role-setting.service'
import 'styles/setting/role-setting/finance-role/finance-role-detail.scss'

class FinanceRoleDetail extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      updateLoading: false,
      updateState: false,
      infoList: [
        {type: 'input', id: 'roleID', label: messages('setting.key935'/*角色编号*/), disabled: true, isRequired: true},
        {type: 'input', id: 'roleName', label: messages('setting.key936'/*角色名称*/), isRequired: true},
        {type: 'input', id: 'roleDescription', label: messages('setting.key937'/*角色说明*/)},
        {type: 'date', id: 'createdDate', label: messages('setting.key938'/*创建日期*/), showTime: true, disabled: true},
        {type: 'input', id: 'createBy', label: messages('setting.key939'/*创建人*/), disabled: true},
        {type: 'date', id: 'modifyDate', label: messages('setting.key940'/*最近修改时间*/), showTime: true, disabled: true},
        {type: 'input', id: 'modifyBy', label: messages('setting.key941'/*最近修改人*/), disabled: true},
        {type: 'input', id: 'modifyRecordInfo', label: messages('setting.key942'/*最近修改记录*/), disabled: true},
      ],
      infoData: {},
      roleInfo: {},
      roleSettingFinance: menuRoute.getRouteItem('role-setting-finance'),
    }
  }

  componentDidMount() {
    this.getBasicInfo()
  }

  componentWillReceiveProps(nextProps) {

  }

  //获取基本信息
  getBasicInfo = () => {
    let roleOID =  this.props.params.roleOID;
    RSService.getFinanceRoleDetail(roleOID).then(res => {
      let infoData = res.data.roleBaseInfo;
      infoData.modifyRecordInfo = constants.getTextByValue(infoData.modifyRecord, 'modifyRecord');
      //给一个默认值
      let roleInfo = res.data;
      if(roleInfo.pageRoles.length === 0)
        roleInfo.pageRoles = [
          {"roleOID":roleOID,"pageName":"RESTAURANTLOANANDREPAYVIEW","action":0},
          {"roleOID":roleOID,"pageName":"REPAYMENTSLIPMANAGEMENT","action":0},
          {"roleOID":roleOID,"pageName":"FINANCEMANAGEMENT","action":0},
          {"roleOID":roleOID,"pageName":"LOANGLOBALVIEW","action":0},
          {"roleOID":roleOID,"pageName":"PAYMENTBATCH","action":0},
          {"roleOID":roleOID,"pageName":"BUDGETAMOUNT","action":0},
          {"roleOID":roleOID,"pageName":"RECEIPTEDINVOICES","action":0},
          {"roleOID":roleOID,"pageName":"EXPENSEAPPLICATIONVIEW","action":2},
          {"roleOID":roleOID,"pageName":"EXPENSEVIEW","action":0},
          {"roleOID":roleOID,"pageName":"VENDORMAINTENANCE","action":0},
          {"roleOID":roleOID,"pageName":"LOANBILLMANAGEMENT","action":0},
          {"roleOID":roleOID,"pageName":"BUDGETJOURNALREVIEW","action":0},
          {"roleOID":roleOID,"pageName":"EXPENSEPAYMENT","action":0},
          {"roleOID":roleOID,"pageName":"BUDGETOCCUPANCYADJUSTMENT","action":0},
          {"roleOID":roleOID,"pageName":"EXPENSEAUDIT","action":0},
          {"roleOID":roleOID,"pageName":"PRINTINVOICEBATCH","action":0},
          {"roleOID":roleOID,"pageName":"BUDGETJOURNAL","action":0},
          {"roleOID":roleOID,"pageName":"VENDORRECONCILIATION","action":0},
          {"roleOID":roleOID,"pageName":"BUDGET","action":0},
          {"roleOID":roleOID,"pageName":"BUDGETBALANCEPLANINQUIRY","action":0},
          {"roleOID":roleOID,"pageName":"LOANMANAGEMENT","action":0}];
      this.setState({ infoData, roleInfo })
    })
  };

  //更新基本信息
  updateBasicInfo = (values) => {
    if (values.roleName && values.roleName.length > 20) {
      message.error(`${messages('setting.key936'/*角色名称*/)} ${messages('common.max.characters.length', {max: 20})}`);
      return false
    } else if (values.roleDescription && values.roleDescription.length > 100) {
      message.error(`${messages('setting.key937'/*角色说明*/)} ${messages('common.max.characters.length', {max: 100})}`);
      return false
    } else {
      let params = {
        roleOID: this.props.params.roleOID,
        roleName: values.roleName,
        roleDescription: values.roleDescription
      };
      this.setState({ updateLoading: true });
      RSService.updateFinanceRole(params).then(res => {
        let infoData = res.data;
        infoData.modifyRecordInfo = constants.getTextByValue(infoData.modifyRecord, 'modifyRecord');
        this.setState({ infoData },() => {
          this.setState({ updateLoading: false, updateState: true });
          message.success(messages('common.save.success', {name: ''}))
        })
      }).catch(() => {
        this.setState({ updateLoading: false, updateState: false });
      })
    }
  };

  //返回
  goBack = () => {
    this.context.router.push(this.state.roleSettingFinance.url)
  };

  render() {
    const { loading, updateLoading, updateState, infoList, infoData, roleInfo } = this.state;
    return (
      <div className="finance-role-detail">
        <BasicInfo infoList={infoList}
                   infoData={infoData}
                   loading={updateLoading}
                   updateState={updateState}
                   updateHandle={this.updateBasicInfo}
        />
        <Tabs defaultActiveKey="pageAuthInfo">
          <TabPane tab={messages('setting.key943'/*页面权限*/)} key="pageAuthInfo">
            <DetailPageAuth pageRoles={roleInfo.pageRoles} roleBaseInfo={infoData} roleOID={this.props.params.roleOID}/>
          </TabPane>
          <TabPane tab={messages('setting.key944'/*法人实体*/)} key="legalEntity">
            <DetailLegalEntity corporationEntity={roleInfo.corporationEntity} roleBaseInfo={infoData} roleOID={this.props.params.roleOID}/>
          </TabPane>
          {/*<TabPane tab="公司权限" key="companyAuth"><DetailCompanyAuth/></TabPane>*/}
          <TabPane tab={messages('setting.key945'/*部门权限*/)} key="departmentAuth">
            <DetailDepartmentAuth department={roleInfo.department} roleBaseInfo={infoData} roleOID={this.props.params.roleOID}/>
          </TabPane>
          <TabPane tab={messages('setting.key903'/*成本中心*/)} key="costCenter">
            <DetailCostCenter roleBaseInfo={infoData} roleOID={this.props.params.roleOID}/>
          </TabPane>
          <TabPane tab={messages('setting.key946'/*分配人员*/)} key="assignUserInfo">
            <DetailAssignCenter roleBaseInfo={infoData} roleOID={this.props.params.roleOID}/>
          </TabPane>
        </Tabs>
        <a className="back" onClick={this.goBack}>
          <Icon type="rollback" style={{marginRight: 5}}/>
          {messages('common.back')}
        </a>
      </div>
    )
  }
}

FinanceRoleDetail.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    profile: state.login.profile,
  }
}

const wrappedFinanceRoleDetail = Form.create()(FinanceRoleDetail);

export default connect(mapStateToProps)(wrappedFinanceRoleDetail)
